package com.qikux.controller;


import cn.hutool.core.io.FileUtil;
import com.qikux.annotation.*;
import com.qikux.consts.AppConst;
import com.qikux.entity.User;
import com.qikux.service.UserService;
import com.qikux.utils.ContextUtils;
import com.qikux.utils.UploadUtils;
import com.qikux.vo.HttpEntity;
import com.qikux.vo.Page;
import com.qikux.vo.ResultVo;

import javax.net.ssl.HandshakeCompletedEvent;

import java.io.File;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("")
public class AttentionController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/attention",method = RequestMethod.GET)
    public String attention(){ return "attention";}

    @ResponseBody
    @RequestMapping(value = "/detail/attention",method = RequestMethod.GET)
    public ResultVo<Page<Map<String, Object>>> detailAttention(){
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);

        Long userId = user.getId();

        // 分页查询, 获取页码
        String page = ContextUtils.getRequest().getParameter("page");
        if (page == null || "".equals(page)) page = "1";
        int _page = Integer.parseInt(page);

        /**
         * 包含了分页参数
         */
        Page<Map<String,Object>> pageBean = Page.builder().rows(10).page(_page).build();
        // 调用业务逻辑层处理业务
        return userService.detailAttention(userId,pageBean);
    }

    @ResponseBody
    @RequestMapping(value = "/fenNum",method = RequestMethod.GET)
    public ResultVo<List<Map<String,Object>>> fenNum(){
        // 获取当前用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        // 调用业务逻辑层处理业务
        return userService.fenNum(user.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/fenNum2",method = RequestMethod.GET)
    public ResultVo<List<Map<String,Object>>> fenNum2(){
        // 获取当前用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        Long userId = user.getId();
        // 调用业务逻辑层处理业务
        return userService.fenNum2(userId);
    }

    @ResponseBody
    @RequestMapping(value = "/updateStatus",method = RequestMethod.GET)
    public int uploadStatus(){
        // 获取页面传的id
        Long uid = Long.parseLong(ContextUtils.getRequest().getParameter("uid"));
        // 调用业务逻辑层处理业务
        return userService.uploadStatusById(uid);
    }

    @RequestMapping(value = "/photo",method = RequestMethod.GET)
    public HttpEntity seePhoto(){
        File file = null;
        // 获取传入的图片地址
        String photoPath = ContextUtils.getRequest().getParameter("p");

        // 判断地址是否为空
        if (photoPath==null || "".equals(photoPath)){
            String realPath = ContextUtils.getRequest().getRealPath("images/unknown.png");
            file = new File(realPath);
        }else {
            file = UploadUtils.findFile("upload.photo.dir", photoPath);
        }

        return new HttpEntity(FileUtil.readBytes(file),"",false);
    }

    @RequestMapping("/addAttention")
    @ResponseBody
    public ResultVo<?> addAttention(){
        // 获取当前登录用户
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        // 获取登录用户的id
        Long userId = user.getId();
        // 获取页面关注用户的id
        Long uid = Long.parseLong(ContextUtils.getRequest().getParameter("uid"));
        // 调用业务逻辑层处理业务
        return userService.addAttention(userId,uid);
    }

    @ResponseBody
    @RequestMapping("/deleteAttention")
    public ResultVo<?> deleteAttention(){
        // 获取当前登录用户
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        // 获取登录用户的id
        Long userId = user.getId();
        System.out.println(userId);
        // 获取页面关注用户的id
        Long uid = Long.parseLong(ContextUtils.getRequest().getParameter("uid"));
        // 调用业务逻辑层处理业务
        return userService.deleteAttention(userId,uid);
    }

    @ResponseBody
    @RequestMapping("/showAttention")
    public ResultVo<Map<String,Object>> showAttention(){
        // 获取当前登录的用户id
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        Long userId = user.getId();
        // 获取资源上传者的id
        Long rId = Long.parseLong(ContextUtils.getRequest().getParameter("rid"));
        if (userId.equals(rId)){
            return null;
        }
        // 调用业务逻辑层处理业务
        return userService.showAttention(userId,rId);
    }
}
